#include "stdafx.h"
#include "VideoPlayer.h"
CVideoPlayer::CVideoPlayer(){
	m_graphBuilder=NULL;
	m_MediaCtrl=NULL;
	m_MediaPsn=NULL;
	m_VideoWnd=NULL;
	CoInitialize(NULL);
	CoCreateInstance(CLSID_FilterGraph,0,CLSCTX_INPROC_SERVER,IID_IGraphBuilder,(void **)&m_graphBuilder);
	m_graphBuilder->QueryInterface(IID_IMediaControl,(void**)&m_MediaCtrl);
	m_graphBuilder->QueryInterface(IID_IMediaPosition,(void**)&m_MediaPsn);
	m_graphBuilder->QueryInterface(IID_IVideoWindow,(void**)&m_VideoWnd);
	isplay=FALSE;
	//m_graphBuilder->RenderFile(fileName,NULL);
}
CVideoPlayer::~CVideoPlayer(){
	m_graphBuilder->Release();
	m_MediaCtrl->Release();
	m_MediaPsn->Release();
	m_VideoWnd->Release();
}
void CVideoPlayer::Play(HWND hWnd, TCHAR *fileName){
	m_graphBuilder->RenderFile(fileName,NULL);
	m_VideoWnd->put_Owner((OAHWND)hWnd);
	m_VideoWnd->put_WindowStyle(WS_CHILD|WS_CLIPSIBLINGS|WS_CLIPCHILDREN);
	RECT rect;
	GetClientRect(hWnd,&rect);
	m_VideoWnd->SetWindowPosition(rect.left,rect.top,rect.right,rect.bottom);
	m_VideoWnd->put_Visible(OATRUE);
	m_VideoWnd->put_MessageDrain((OAHWND)hWnd);
	m_MediaPsn->put_CurrentPosition(0);
	//m_MediaPsn->put_Rate(2.0);
	m_MediaCtrl->Run();
	isplay=TRUE;
}
void CVideoPlayer::Stop(){
	m_MediaCtrl->Stop();
	isplay=FALSE;
}
BOOL CVideoPlayer::IsEnd(){
	double current;
	double total;
	m_MediaPsn->get_CurrentPosition(&current);
	m_MediaPsn->get_Duration(&total);
	if(current>=total){
		isplay=FALSE;
		return TRUE;
	}else{
		return FALSE;
	}
}
BOOL CVideoPlayer::IsPlaying(){
	return isplay;
}